var companyId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.companyId);
var schoolId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolId);
var schoolMap = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolMap);

var app = new Vue({
    el: '#app',
    data: {
        permissions: ['rules:add',"course:list"],//本页权限数组
        beginDate: '',
        endDate: '',
        courseList: [],
        classList: [],
        timeList: [],
        isChange: 0,
        notifyType: 0,
        msg: '',
        selects: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
        selectId:null,
        selectType:null
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        that.getCourseList();
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if (empty(token)) {
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon: 5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            }, 1200);

            return;
        }

    },
    methods: {
        //权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        // 得到班级
        getCourseList: function () {
            var that = this;
            Get(that, _HOST + _API.organization_course_list + "?schoolId=" + schoolId + "&companyId=" + companyId,that.permissions[1])
                .then(function (response) {
                    // 响应成功回调
                    if (response) {
                        that.courseList = response.rows;
                        console.log(this.courseList);
                    }
                });
        },

        addRecord: function () {
            var that = this;
            var rulesTable = $("#rulesTable");
            rulesTable.append("<tr class=\"record\"><td class=\"ruleName\" contenteditable=\"true\" width=\"20%\"></td>" +
                "<td class=\"val\" contenteditable=\"true\" width=\"20%\"></td></tr>");
        },
        decreaseRecord: function () {
            var record = $(".record");
            record.last().remove();
        },
        submit:function () {
            var that=this;
            var resultForm=[];
            let nodes = $('.record');
            var type=that.selectType;
            var courseId=that.selectId;
            nodes.each(function () {
                var currentNode= $(this);
                var cells=currentNode.context.cells;
                var temp={};
                temp["ruleName"]=cells[0].innerText;
                temp["val"]=cells[1].innerText;
                temp["companyId"]=companyId;
                temp["schoolId"]=schoolId;
                temp["type"]=type;
                temp["status"]=1;
                temp["courseId"]=that.selectId;
                resultForm.push(temp);
            });
            console.log(resultForm);

            Post(that,_HOST+_API.organization_rules_add,resultForm,true,that.permissions[0])
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if(response){
                        if(response.success){
                            layer.msg(response.msg, {time: 1500, icon:1});
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                layer.closeAll();
                            }, 1500)
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                })
        }
    },
});

/*Validator();

function Validator() {
    $("#Form").submit(function (e) {
        e.preventDefault();
        layer.open({
            type: 1,
            content: $('#haveChanged')
        });
    });
}*/
